package com.gxa.shop.dao.address.impl;

import com.gxa.shop.bean.dto.AddressDto;
import com.gxa.shop.bean.dto.AddressQDto;
import com.gxa.shop.bean.pojo.Address;
import com.gxa.shop.bean.vo.AddressQ;
import com.gxa.shop.bean.vo.AddressVo;
import com.gxa.shop.bean.vo.ReginVo;
import com.gxa.shop.commons.jdbc.JDBCUtil;
import com.gxa.shop.commons.r.R;
import com.gxa.shop.dao.address.AddressDao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class AddressDaoImpl implements AddressDao {

    /**
     * 查询所有收货地址
     * @return all收货地址
     */
    @Override
    public List<AddressVo> selectAllAddress(AddressDto addressDto) {
        JDBCUtil jdbcUtil=new JDBCUtil();
        StringBuffer sql=new StringBuffer(" SELECT a.address_id,ac.username, a.recipient_name, a.recipient_phone, r.regin, a.recipient_address " +
                " FROM address a " +
                " JOIN account ac ON a.user_id = ac.user_id " +
                " JOIN regin r on a.expressfee_id=r.expressfee_id " +
                " WHERE 1=1 ");
        List<Object> objects=new ArrayList<>();
        if(addressDto.getAddressName()!=null){
            sql.append(" and ac.username like concat ('%',?,'%') ");
            objects.add(addressDto.getAddressName());

        }
        if(addressDto.getExpressfeeId()!=null){
            sql.append(" and a.expressfee_id like concat ('%',?,'%')");
            objects.add(addressDto.getExpressfeeId());
        }
        sql.append(" order  by a.address_id  desc limit " + (addressDto.getPage()-1)*addressDto.getLimit() + "," + addressDto.getLimit());
        List<Map<String, Object>> mapList = jdbcUtil.query(sql.toString(),objects.toArray());
        List<AddressVo> list=new ArrayList<>();

            for (Map<String, Object> map:mapList){
                AddressVo address=new AddressVo();
                address.setAddressId((Integer) map.get("address_id"));
                address.setUserName((String)  map.get("username"));
                address.setRecipientName((String) map.get("recipient_name"));
                address.setRecipientPhone((String) map.get("recipient_phone"));
                address.setRecipientRegion((String) map.get("regin"));
                address.setRecipientAddress((String) map.get("recipient_address"));
                list.add(address);
            }
        return list;
    }

    @Override
    public int deleteAddr(Integer id) {
        JDBCUtil jdbcUtil=new JDBCUtil();
        String sql="delete from address where address_id=?";
        return jdbcUtil.update(sql,id);
    }

    @Override
    public int updateAddr(Address address) {
        JDBCUtil jdbcUtil=new JDBCUtil();
        String sql=" UPDATE address SET recipient_name=?, recipient_phone=?, expressfee_id=?, recipient_address=? WHERE address_id=? AND user_id=?";
        return jdbcUtil.update(sql,address.getRecipientName(),address.getRecipientPhone(),address.getExpressfeeId(),address.getRecipientAddress(),address.getAddressId(),address.getUserId());
    }

    @Override
    public int addAddr(Address address) {
        JDBCUtil jdbcUtil=new JDBCUtil();
        String sql="INSERT INTO address (user_id, recipient_name, recipient_phone, expressfee_id, recipient_address) VALUES (?, ?, ?, ?, ?);";
        return jdbcUtil.update(sql,address.getUserId(),address.getRecipientName(),address.getRecipientPhone(),address.getExpressfeeId(),address.getRecipientAddress());
    }

    @Override
    public Long totalAllAddress(AddressDto addressDto) {
        JDBCUtil jdbcUtil=new JDBCUtil();
        StringBuffer sql=new StringBuffer(" SELECT count(*) as total " +
                " FROM address a " +
                " JOIN account ac ON a.user_id = ac.user_id " +
                " JOIN regin r on a.expressfee_id=r.expressfee_id " +
                " WHERE 1=1 ");
        List<Object> objects=new ArrayList<>();
        if(addressDto.getAddressName()!=null){

            sql.append(" and ac.username like concat ('%',?,'%') ");
            objects.add(addressDto.getAddressName());

        }
        if(addressDto.getExpressfeeId()!=null){
            sql.append(" and a.expressfee_id = ?");
            objects.add(addressDto.getExpressfeeId());
        }
        List<Map<String,Object>> maps=jdbcUtil.query(sql.toString(),objects.toArray());
        Long ct = 0L;
        if (maps.size() > 0) {
            //该变
            ct = (Long) maps.get(0).get("total");
        }
        return ct;
    }

    @Override
    public List<ReginVo> selectAllRegin() {
        JDBCUtil jdbcUtil=new JDBCUtil();
        String sql="select expressfee_id,regin from regin where 1=1";
        List<Map<String,Object>> list=jdbcUtil.query(sql);
        List<ReginVo> reginVos=new ArrayList<>();
        for (Map<String,Object> map:list){
            ReginVo reginVo=new ReginVo();
            reginVo.setExpressfeeId((Integer) map.get("expressfee_id"));
            reginVo.setRegin((String) map.get("regin"));
            reginVos.add(reginVo);
        }
        return reginVos;
    }

    @Override
    public List<AddressQ> selectAddr(Integer id) {
        JDBCUtil jdbcUtil=new JDBCUtil();
        String sql="select a.address_id, a.recipient_name, a.recipient_phone, r.regin, a.recipient_address from address a " +
                " left join regin r on a.expressfee_id=r.expressfee_id " +
                "where a.user_id=? ";
        List<Map<String,Object>> maps=jdbcUtil.query(sql,id);
        List<AddressQ> list=new ArrayList<>();
        for (Map<String,Object> map:maps){
            AddressQ addressQ=new AddressQ();
            addressQ.setAddressId((Integer) map.get("address_id"));
            addressQ.setRecipientName((String) map.get("recipient_name"));
            addressQ.setRecipientPhone((String) map.get("recipient_phone"));
            addressQ.setRecipientRegion((String) map.get("regin"));
            addressQ.setRecipientAddress((String) map.get("recipient_address"));
            list.add(addressQ);
        }
        return list;
    }
}
